package com.dropdb.demo.service.impl;

import com.dropdb.demo.bean.bo.BaseData;
import com.dropdb.demo.bean.User;
import com.dropdb.demo.bean.UserExample;
import com.dropdb.demo.mapper.UserMapper;
import com.dropdb.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author:tanxin Desc:
 * @date:2020/6/26 16:38
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;
    @Override
    public BaseData queryUsers(Integer page, Integer limit, String sort, String order,String username,String mobile) {

        UserExample userExample = new UserExample();
        //设置排序方式和排序类型
        userExample.setOrderByClause(sort + " " + order);
        //条件查询
        if (username != null && mobile != null) {
            userExample.createCriteria().andMobileLike("%" + mobile + "%").andUsernameLike("%" + username + "%");
        }else if (mobile != null) {
            userExample.createCriteria().andMobileLike("%" + mobile + "%");
        }else if (username != null) {
            userExample.createCriteria().andUsernameLike("%" + username + "%");
        }
        //执行查询之前使用分页
        PageHelper.startPage(page,limit);
        //查询到用户list
        List<User> users = userMapper.selectByExample(userExample);
        //将查询到的list进行分页
        PageInfo<User> userPageInfo = new PageInfo<>(users);

        long total = userPageInfo.getTotal();
        //返回用户信息和分页条数
        return new BaseData(total,users);
    }
}
